// dnSpy decompiler from Assembly-CSharp.dll class: TiledAutoscaledBg
using System;
using UnityEngine;

public class TiledAutoscaledBg : CheckedMonoBehaviour
{
	protected override void OnAwake()
	{
		this.numTiles = this.tilesColumns * this.tilesRows;
		if (this.texBgTiles.Length < this.numTiles)
		{
			UnityEngine.Debug.LogError("texBgTiles must have at least " + this.numTiles + " tiles!");
		}
		base.OnAwake();
		this.DetermineBgSize();
		this.InitBgTiles();
	}

	private void DetermineBgSize()
	{
		int width = Screen.width;
		int height = Screen.height;
		this.ApplyWorkaroundForIOSScreenRotationBug(ref width, ref height);
		this.bgWidth = (float)width;
		this.bgHeight = this.bgWidth * this.baseBgHeight / this.baseBgWidth;
		if (this.bgHeight < (float)height)
		{
			this.bgHeight = (float)height;
			this.bgWidth = this.bgHeight * this.baseBgWidth / this.baseBgHeight;
		}
		this.bgLeft = (float)width * 0.5f - this.bgWidth * 0.5f;
		this.bgTop = (float)height * 0.5f - this.bgHeight * 0.5f;
		this.wasBgSizeDetermined = true;
	}

	private void ApplyWorkaroundForIOSScreenRotationBug(ref int screenWidth, ref int screenHeight)
	{
		if (this.baseBgWidth > this.baseBgHeight && Application.platform == RuntimePlatform.IPhonePlayer && screenWidth < screenHeight)
		{
			UnityEngine.Debug.Log("Applying workaround for Unity bug on iOS: screen orientation was falsely reported as portrait");
			int num = screenWidth;
			screenWidth = screenHeight;
			screenHeight = num;
		}
	}

	private void InitBgTiles()
	{
		this.rectBgTiles = new Rect[this.numTiles];
		float num = this.bgWidth / (float)this.tilesColumns;
		float num2 = this.bgHeight / (float)this.tilesRows;
		int num3 = 0;
		for (int i = 0; i < this.tilesRows; i++)
		{
			int j = 0;
			while (j < this.tilesColumns)
			{
				this.rectBgTiles[num3] = new Rect(this.bgLeft + num * (float)j, this.bgTop + num2 * (float)i, num, num2);
				j++;
				num3++;
			}
		}
	}

	public Rect GetRectFromBaseRect(Rect rectBase)
	{
		if (!this.wasBgSizeDetermined)
		{
			this.DetermineBgSize();
		}
		float x = rectBase.xMin / this.baseBgWidth * this.bgWidth + this.bgLeft;
		float y = rectBase.yMin / this.baseBgHeight * this.bgHeight + this.bgTop;
		float width = rectBase.width / this.baseBgWidth * this.bgWidth;
		float height = rectBase.height / this.baseBgHeight * this.bgHeight;
		return new Rect(x, y, width, height);
	}

	private void OnGUI()
	{
		GUI.depth = 100;
		for (int i = 0; i < this.numTiles; i++)
		{
			GUI.DrawTexture(this.rectBgTiles[i], this.texBgTiles[i]);
		}
	}

	public float baseBgWidth = 4096f;

	public float baseBgHeight = 3072f;

	public int tilesColumns = 2;

	public int tilesRows = 1;

	private int numTiles;

	public Texture2D[] texBgTiles;

	private Rect[] rectBgTiles;

	private float bgWidth;

	private float bgHeight;

	private float bgLeft;

	private float bgTop;

	private bool wasBgSizeDetermined;
}
